package com.slm.report.view.data;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.CustomListener;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.blankj.utilcode.util.StringUtils;
import com.slm.report.R;
import com.slm.report.databinding.ViewTextContentBinding;

/**
 * @ProjectName: My Application
 * @Package: com.slm.report.view.data
 * @ClassName: ChoiceContentView
 * @Description: java类作用描述
 * @Author: 王丹丹
 * @CreateDate: 2020/6/2 10:52
 * @UpdateUser: 更新者
 * @UpdateDate: 2020/6/2 10:52
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class ChoiceContentView extends BaseContentView {
    private ViewTextContentBinding viewTextContentBinding = null;
    private OptionsPickerView optionsPickerView;

    public ChoiceContentView(Context context) {
        super(context);
        viewTextContentBinding = ViewTextContentBinding.inflate(LayoutInflater.from(getContext()), this, true);


    }

    @Override
    void upDataContent() {
        viewTextContentBinding.titleTv.setText(contentDataBean.getTitleStr());
        if (isEdit && StringUtils.isEmpty(contentDataBean.getContent())) {
            viewTextContentBinding.contentTv.setText("请选择" + contentDataBean.getTitleStr());
            viewTextContentBinding.contentTv.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    initCustomOptionPicker();
                    optionsPickerView.show();
                }
            });
        } else {
            viewTextContentBinding.contentTv.setText(contentDataBean.getContent());
            viewTextContentBinding.contentTv.setOnClickListener(null);
        }

    }


    private void initCustomOptionPicker() {//条件选择器初始化，自定义布局
        optionsPickerView = new OptionsPickerBuilder(mContext, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3, View v) {
                String tx = contentDataBean.getChoiceItems().get(options1);
                viewTextContentBinding.contentTv.setText(tx);
                contentDataBean.setContent(tx);
                viewTextContentBinding.contentTv.setTextColor(ContextCompat.getColor(mContext, R.color.colorPrimary));
            }
        })
                .setLayoutRes(R.layout.choice_view_layout, new CustomListener() {
                    @Override
                    public void customLayout(View v) {
                        final TextView tvSubmit = v.findViewById(R.id.tv_sure);
                        final TextView tvCancel = v.findViewById(R.id.tv_cancel);
                        tvSubmit.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                optionsPickerView.returnData();
                                optionsPickerView.dismiss();
                            }
                        });

                        tvCancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                optionsPickerView.dismiss();
                            }
                        });
                    }
                })
                .isDialog(true)
                .setOutSideCancelable(false)
                .build();

        optionsPickerView.setPicker(contentDataBean.getChoiceItems());


    }
}
